/**
 * 
 */
package edu.pw.treegrid.server.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import org.apache.log4j.Logger;

import javax.sql.DataSource;

import edu.pw.treegrid.server.query.QueryTree;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author Piotrek
 */
@Deprecated
public class DatabaseConnector {
	private static Logger log = Logger.getLogger("xalan.reports");
	
	private DataSource dataSource;
	private Connection connection;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
		try {
			this.connection = this.dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	public PreparedStatement fetch(QueryTree queryTree) throws SQLException {
		String sql = queryTree.generateQuery().getQuery();
		log.info(sql);
		PreparedStatement stm = connection.prepareStatement(sql);
		Map<Integer, Object> parameters = queryTree.getParameters();
		for(Integer parameterIndex: parameters.keySet()) {
			stm.setObject(parameterIndex, parameters.get(parameterIndex));
		}
		return stm;
	}
}